<?php
/**
  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; version 3 of the License.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.
 */
/**
 * Youtube API class
 * @package Util
 * @author Somlyai Dezső
 * @author Vincze Tamás
 * @version 0.1
 */
namespace Util;
class Youtube {

	/**
	 * Baselink for youtube API
	 * @var string
	 */
	protected $_link = "http://gdata.youtube.com/feeds/api/videos";
	
	/**
	 * Internal parameters fro the util
	 * @var array
	 */
	protected $_params = array();
	
	/**
	 * Get data of a chanel, with search, and limit functions
	 * @param string $url full URL, or only the name of the chanel
	 * @param string $search search IN the chanel's videos
	 * @param int $start offset of the videos limit
	 * @param int $stop max video result count of the videos limit
	 * @param string $format json|XML|atom default:json
	 * @return string json|XML|atom
	 */
	public function getChannel($url, $search = null, $start = 1, $stop = 25, $format = "json") {
		$this->_params["start-index"] = $start == 1 ? null : $start;
		$this->_params["max-results"] = $stop == 25 ? null : $stop;
		$this->_params["alt"] = $format;
		$this->_params["author"] = end(explode("/", $url));
		$this->_params["q"] = $search;
		$this->_params["orderby"] = "published";
		return $this->_getData();
	}
	
	/**
	 * Search videos from youtube, with and limit functions
	 * @param string $search search IN the youtube's videos
	 * @param int $start offset of the videos limit
	 * @param int $stop max video result count of the videos limit
	 * @param string $format json|XML|atom default:json
	 * @return string json|XML|atom
	 */
	public function search($search = null, $start = 1, $stop = 25, $format = "json") {
		$this->_params["start-index"] = $start == 1 ? null : $start;
		$this->_params["max-results"] = $stop == 25 ? null : $stop;
		$this->_params["alt"] = $format;
		$this->_params["q"] = $search;
		return $this->_getData();
	}
	
	public function getEntries($url = null, $search = null, $start = 1, $stop = 25, $entryData = array("id", "title", "viewCount", "iframe")){
		$data = (is_null($url)) ? $this->search($search, $start, $stop) : $this->getChannel($url, $search, $start, $stop);
		$feed = json_decode($data)->feed;
		
		$return = array();
		foreach ($feed as $feedItem) {
			var_dump($feedItem);
		}
		return $return;
	}
	
	/**
	 * Gets the result data from an URL
	 * @return string
	 */
	protected function _getData() {
		return file_get_contents($this->_link . "?" . http_build_query($this->_params));
	}
	
}

?>
